package cn.xshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.xshi.sys.model.SysAreaRegion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseZTreeEntity;
import cn.xshi.common.idgeneration.SnowflakeIdWorker;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysAreaRegionService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Desc 行政区划
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysAreaRegion")
@Api(value = "行政区划API",tags = "行政区划API",description = "行政区划API")
public class SysAreaRegionController extends BaseAction {

	@Resource
	private SysAreaRegionService sysAreaRegionService;

	@Resource
	SnowflakeIdWorker snowflakeIdWorker;

	/**
	* 查询行政区划
	* @param request 
	*/
	@ApiOperation(value="查询行政区划", notes="查询行政区划")
	@GetMapping(value="/list")
	@Auth(value = "/sysAreaRegion/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getSysAreaRegionListByCondition(HttpServletRequest request){
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		Map<String,Object> condition = new HashMap<String,Object>();
		List<SysAreaRegion> sysAreaRegionList = sysAreaRegionService.getSysAreaRegionListByCondition(condition);
		for(int i = 0; i < sysAreaRegionList.size(); i++){
			SysAreaRegion sysAreaRegion = sysAreaRegionList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(sysAreaRegion.getId());
			BaseZTreeEntity.setPId(""+ sysAreaRegion.getParentId());
			BaseZTreeEntity.setText(sysAreaRegion.getName());
			BaseZTreeEntity.setName(sysAreaRegion.getName());
			BaseZTreeEntity.setTempObject(""+ sysAreaRegion.getRegionLevel());
			BaseZTreeEntity.setContent(sysAreaRegion.getNameEn());
			BaseZTreeEntity.setIntegerappend("经度："+ sysAreaRegion.getLongitude()+"<br>纬度："+ sysAreaRegion.getLatitude());
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
	}

	/**
	* 查询单个行政区划
	* @param id
	*/
	@ApiOperation(value="查询单个行政区划", notes="根据Id查询单个行政区划")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysAreaRegion/get",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult<SysAreaRegion> getSysAreaRegionById(@PathVariable("id") String id){
		SysAreaRegion sysAreaRegion = sysAreaRegionService.getSysAreaRegionById(id);
		return outDataStr(sysAreaRegion);
	}

	/**
	 *查询全部行政区划
	 * @return
	 */
	@ApiOperation(value="查询全部行政区划", notes="查询全部行政区划")
	@GetMapping(value="/lists")
	@Auth(value = "/sysAreaRegion/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult lists(){
		Map<String,Object> condition = new HashMap<String,Object>();
		List<SysAreaRegion> sysAreaRegionList = sysAreaRegionService.getSysAreaRegionListByCondition(condition);
		return outDataStr(sysAreaRegionList);
	}

	/**
	* 添加
	* @param sysAreaRegion
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建行政区划", notes="创建行政区划")
	public BaseResult addSysAreaRegion(@RequestBody SysAreaRegion sysAreaRegion){
		int i = 0;
		if(null != sysAreaRegion){
			sysAreaRegion.setId(""+snowflakeIdWorker.nextId());
			sysAreaRegion.setCreateId(getCurrentAccountId());
			sysAreaRegion.setCreateTime(getDate());
			i= sysAreaRegionService.addSysAreaRegion(sysAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param sysAreaRegion
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑行政区划", notes="编辑行政区划")
	public BaseResult updateSysAreaRegion(@RequestBody SysAreaRegion sysAreaRegion){
		int i = 0;
		if(!StringUtil.isEmpty(sysAreaRegion.getId())){
			sysAreaRegion.setUpdateId(getCurrentAccountId());
			sysAreaRegion.setUpdateTime(getDate());
			i= sysAreaRegionService.updateSysAreaRegion(sysAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除行政区划", notes="删除行政区划")
	public BaseResult delSysAreaRegion(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysAreaRegionService.delSysAreaRegion(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 复制一行并生成记录
	* @param ID
	*/
	@GetMapping(value="/copy")
	@ApiOperation(value="复制并生成行政区划", notes="复制并生成删除行政区划")
	public BaseResult copySysAreaRegion(String ID){
		int i = 0;
		SysAreaRegion sysAreaRegion = sysAreaRegionService.getSysAreaRegionById(ID);
		if(null != sysAreaRegion){
			sysAreaRegion.setId(""+snowflakeIdWorker.nextId());
			sysAreaRegion.setCreateId(getCurrentAccountId());
			sysAreaRegion.setCreateTime(getDate());
			i= sysAreaRegionService.addSysAreaRegion(sysAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
